package com.wisdom.benefit.jufa.product;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.wisdom.benefit.jufa.event.CallBackInterface;
import com.wisdom.benefit.jufa.main.HomeFragment;
import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseApplication;
import com.wisdom.benefit.jufa.base.BaseNewFragment;
import com.wisdom.benefit.jufa.bean.HomeListBean;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.CommonUtils;
import com.wisdom.benefit.jufa.utils.DataCleanManager;
import com.wisdom.benefit.jufa.utils.DialogUtil;
import com.wisdom.benefit.jufa.widget.Mylistview;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AccountFragment extends BaseNewFragment implements View.OnClickListener, CallBackInterface {
    public static final String TAG = HomeFragment.class.getSimpleName();
    private ArrayList<HomeListBean> datas;
    private AccountAdapter accountAdapter;
    private Mylistview lv;
    private Button btn;

    public static AccountFragment newInstance() {
        Bundle args = new Bundle();
        AccountFragment fragment = new AccountFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume，为true时，Fragment已经可见
            userPassageway();
            datas = new ArrayList<>();
        } else {
            //相当于Fragment的onPause，为false时，Fragment不可见
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        userPassageway();
        datas = new ArrayList<>();
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_account;
    }

    @Override
    public void findViewById(View view) {
        //收款户列表
        lv = view.findViewById(R.id.account_lv);
        //新增收款账号
        btn = view.findViewById(R.id.account_add_btn);
    }

    @Override
    public void setViewData(View view) {
        //设置列表数据

    }

    @Override
    public void setClickEvent(View view) {
//新增收款账号
        btn.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.account_add_btn:
                Intent intent = new Intent(getActivity(), AddAccountActivity.class);
                startActivity(intent);
                break;

        }
    }


    @Override
    public void callBackClick(int position) {
        Intent intent = new Intent(getActivity(), AccountSettingActivity.class);
        intent.putExtra("isGet", datas.get(position).getStatus());
        intent.putExtra("PassagewayId", datas.get(position).getPassagewayId());
        intent.putExtra("PassagewayType", datas.get(position).getPassagewayType());
        intent.putExtra("userId", datas.get(position).getUserId());
        startActivity(intent);
    }

    @Override
    public void callBackClickOther(int position, boolean isCheck) {
        if (datas.get(position).getStatus() == 1) {
            editPassagewayStatus(datas.get(position).getPassagewayId(), 0);
        } else {
            startDialog(datas.get(position).getPassagewayId(),position);
        }
    }

    /**
     * 关闭/开启当前通道
     */
    private void editPassagewayStatus(Long codeId, int receivingAccount) {
        addDisposable(RetrofitService.getNetService()
                .editPassagewayStatus(Long.valueOf(BaseApplication.getContext().getUserId()).longValue(), codeId, receivingAccount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getCode() == 0) {
                        userPassageway();
                    } else {
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("更改状态失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }

    /**
     * 获取当前固态通道
     */
    private void userPassageway() {
        showProgressDialog("");
        addDisposable(RetrofitService.getNetService()
                .userPassageway(Long.valueOf(BaseApplication.getContext().getUserId()).longValue())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getCode() == 0) {
                        datas = new ArrayList<>();
                        datas = response.getData();
                        //列表
                        accountAdapter = new AccountAdapter(datas, getActivity(), this);
                        lv.setAdapter(accountAdapter);
                    } else {
                        datas = new ArrayList<>();
                        //列表
                        accountAdapter = new AccountAdapter(datas, getActivity(), this);
                        lv.setAdapter(accountAdapter);
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("获取数据失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }

    /**
     * 切换通道
     */
    private void startDialog(Long codeId,int position) {
        DialogUtil dialogUtil = new DialogUtil(getActivity(), getActivity());
        dialogUtil.dialog("提示", "确定使用此通道？", "取消", "确定", "1");
        dialogUtil.setOnItemClickListener(new DialogUtil.OnItemClickListener() {
            @Override
            public void onItemCancelClick() {
                userPassageway();
            }

            @Override
            public void onItemConfirmClick() {


            }

            @Override
            public void onItemValueConfirmClick(String a) {
                boolean b = DataCleanManager.clearAllCache(getActivity());
                if (b == true) {
                    editPassagewayStatus(codeId, 1);
                }
            }
        });
    }

}
